<template>
  <div class="my-publish-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>我的发布</span>
        </div>
      </template>

      <el-table :data="tableData" v-loading="loading">
        <el-table-column prop="infoNo" label="信息编号" width="150" />
        <el-table-column label="类型" width="80">
          <template #default="{ row }">
            <el-tag :type="row.type === 0 ? 'warning' : 'success'">
              {{ row.type === 0 ? '失物' : '招领' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="itemName" label="物品名称" width="150" />
        <el-table-column label="物品类别" width="100">
          <template #default="{ row }">
            {{ getCategoryName(row.category) }}
          </template>
        </el-table-column>
        <el-table-column label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">
              {{ getStatusName(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="locationArea" label="地点" width="120" />
        <el-table-column label="发布时间" width="180">
          <template #default="{ row }">
            {{ formatDateTime(row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="150">
          <template #default="{ row }">
            <el-button type="primary" link @click="viewDetail(row.id)">查看</el-button>
            <el-button 
              type="danger" 
              link 
              @click="handleDelete(row.id)"
              v-if="canDelete(row.status)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination" v-if="total > 0">
        <el-pagination
          v-model:current-page="current"
          v-model:page-size="size"
          :total="total"
          layout="total, prev, pager, next"
          @current-change="loadData"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getMyPublish, deleteItem } from '@/api/item'

const router = useRouter()
const loading = ref(false)
const tableData = ref([])
const total = ref(0)
const current = ref(1)
const size = ref(10)

const categoryNames = ['电子设备', '证件卡类', '学习用品', '生活用品', '衣物鞋帽', '其他']
const statusNames = ['待审核', '已发布', '已认领', '已失效', '审核驳回']

const getCategoryName = (category) => categoryNames[category] || '未知'
const getStatusName = (status) => statusNames[status] || '未知'

const getStatusType = (status) => {
  const types = ['info', 'success', 'success', 'info', 'danger']
  return types[status] || 'info'
}

const canDelete = (status) => {
  return [0, 3, 4].includes(status) // 待审核、已失效、审核驳回
}

const formatDateTime = (dateTime) => {
  return new Date(dateTime).toLocaleString()
}

const viewDetail = (id) => {
  router.push(`/detail/${id}`)
}

const handleDelete = (id) => {
  ElMessageBox.confirm('确定删除该信息吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      await deleteItem(id)
      ElMessage.success('删除成功')
      loadData()
    } catch (error) {
      console.error(error)
    }
  })
}

const loadData = async () => {
  loading.value = true
  try {
    const res = await getMyPublish({ current: current.value, size: size.value })
    tableData.value = res.data.records
    total.value = res.data.total
  } catch (error) {
    console.error(error)
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  loadData()
})
</script>

<style scoped>
.my-publish-container {
  max-width: 1200px;
  margin: 0 auto;
}

.card-header {
  font-size: 18px;
  font-weight: bold;
}

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>

